package com.mall.shopping.services;

import com.mall.shopping.IHomeService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.PanelContentConverter;
import com.mall.shopping.converter.PanelConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelContentItemDto;
import com.mall.shopping.dto.PanelDto;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 谭涛
 */
@Slf4j
@Component
@Service(interfaceClass = IHomeService.class)
public class HomeServiceImpl implements IHomeService {

    @Autowired
    private PanelMapper panelMapper;

    @Autowired
    private PanelContentMapper panelContentMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private PanelConverter panelConverter;

    @Autowired
    private PanelContentConverter panelContentConverter;

    @Override
    public HomePageResponse homepage() {
        HomePageResponse response = new HomePageResponse();
        Set<PanelDto> panelDtoSet = new HashSet<>();

        try {
            //查询tb_panel中所有position为0的信息
            Example paneExample = new Example(Panel.class);
            Example.Criteria criteria = paneExample.createCriteria();
            criteria.andEqualTo("position", 0);
            List<Panel> panels = panelMapper.selectByExample(paneExample);
            List<PanelDto> panelDtos = panelConverter.panels2Dto(panels);

            //遍历，根据panelId 查 panelContent 表的内容
            for (PanelDto panelDto : panelDtos) {
                Integer panelId = panelDto.getId();
                Example panelContentExample = new Example(PanelContent.class);
                panelContentExample.setOrderByClause("sort_order asc");
                panelContentExample.createCriteria().andEqualTo("panelId",panelId);
                List<PanelContent> panelContents = panelContentMapper.selectByExample(panelContentExample);
                List<PanelContentItemDto> panelContentItemDtos = panelContentConverter.panelContents2Dto(panelContents);

                //遍历，根据 productId 查询 tb_item 表中的内容
                for (PanelContentItemDto panelContentItemDto: panelContentItemDtos) {
                    Long productId = panelContentItemDto.getProductId();
                    if (productId == null){ continue;}
                    Item item = itemMapper.selectByPrimaryKey(productId);

                    //赋值
                    panelContentItemDto.setProductName(item.getTitle());
                    panelContentItemDto.setSalePrice(item.getPrice());
                    panelContentItemDto.setSubTitle(item.getSellPoint());

                }

                panelDto.setPanelContentItems(panelContentItemDtos);
                panelDtoSet.add(panelDto);
            }
            response.setPanelContentItemDtos(panelDtoSet);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            log.error("HomeServiceImpl.homepage occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }
}
